package com.hangxintong.service;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hangxintong.common.base.entity.LmUser;
import com.hangxintong.common.feign.MessageNacosFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
* @Description
* @Author  Bz.Mr
* @Date   2021/1/15 14:20
*
*/
@Slf4j
@Component
public class MsgReceiver {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Resource
    private MessageNacosFeign messageNacosFeign;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "sms"),
            exchange = @Exchange(value = "amq.topic", type = ExchangeTypes.TOPIC),
            key = "lianmeng.sms.*"
    ))
    @RabbitHandler
    public void onSms(Map<String, String> smsMap) {
        System.err.println("sms phone: " + smsMap.get("phone"));
        System.err.println("sms code: " + smsMap.get("code"));
        log.info("sms phone:{}, ");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "register"),
            exchange = @Exchange(value = "amq.topic", type = ExchangeTypes.TOPIC),
            key = "#.register"
    ))
    @RabbitHandler
    public void onSms2(Map<String, String> smsMap) {
        System.err.println("--------------------------------------");
        System.err.println("register phone: " + smsMap.get("phone"));
        System.err.println("register code: " + smsMap.get("code"));
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "eventA"),
            exchange = @Exchange(value = "amq.topic", type = ExchangeTypes.TOPIC),
            key = "lianmeng.shenhe.eventA"
    ))
    @RabbitHandler
    public void onEventA(Map<String, String> smsMap) {
        System.err.println("register phone123: " + smsMap.get("phone"));
        System.err.println("register code465: " + smsMap.get("code"));
    }



    //1.需要手动在 RabbitMQ手动先创建好，否则未创建的异常
    @RabbitListener(queues = "hxt-message.loginMsg")
    public void recoverLoginMsg(String message){
        //       Object o = rabbitTemplate.receiveAndConvert("hxt-message.loginMsg");
       log.info("Mq 消息接受者：{}",message);
    }
    //2.自动创建队列
    @RabbitListener(queuesToDeclare = @Queue("hxt-message.myQueue2"))
    public void processTwo(String message){
        log.info("Mq 消息接受者myQueue2：{}",message);
    }


    /**3. 自动创建，Exchage与Queue绑定
     * @Description
     * @Author  Bz.Mr
     * @Date   2021/1/14 18:30
     *  订阅催费阅读信息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("hxtMember.updateMemberCompany"),
            exchange = @Exchange("myExchage")
    ))
    public void sendMembership(String message){
        //转换器
        Object parse = JSONObject.parse(message);
        // 对象--json数据 //将对象转换成json
//        String json=mapper.writeValueAsString(map);
//        messageNacosFeign.editReadStatus(logId,memberId,userName);
        log.info("MQ:editReadStatus：{}",message);
    }

    /**
     * @Description
     * @Author  Bz.Mr
     * @Date   2021/1/14 18:30
     *  用户更新添加新Member 会员单位
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("hxtMember.updateMemberCompany"),
            exchange = @Exchange("hxtMessage")
    ))
    public void updateMemberCompanyReceiver(LmUser user) {
        try {
            messageNacosFeign.saveUserMember(user);
        } catch (Exception e) {
            log.error("===========>MessageNacosFeign:{}", e.getMessage());
        }
    }
}
